<<<<<<< HEAD
"""
混合整数规划
"""

from pulp import *
# 设置对象
prob = LpProblem(name='MIP', sense=LpMinimize)
# 设置变量
VAR_NUM = 3
variables = [LpVariable(name='x%d' % i, lowBound=0) for i in range(1, VAR_NUM)]
variables.append(LpVariable(name='x%d' % VAR_NUM, lowBound=0,
                            upBound=1, cat=const.LpInteger))
# 设置目标函数
c = [-3, -2, -1]
objective = sum([c[i]*variables[i] for i in range(0, VAR_NUM)])
# 约束条件
constraints = []
a1 = [1, 1, 1]
constraints.append(sum([a1[i]*variables[i] for i in range(0, VAR_NUM)]) <= 7)
a2 = [4, 2, 1]
constraints.append(sum([a2[i]*variables[i] for i in range(0, VAR_NUM)]) == 12)
a3 = [1, 0, 0]
constraints.append(sum([a3[i]*variables[i] for i in range(0, VAR_NUM)]) >= 0)
a4 = [0, 1, 0]
constraints.append(sum([a4[i]*variables[i] for i in range(0, VAR_NUM)]) >= 0)
# 载入变量
prob += objective
for cons in constraints:
    prob += cons
# 求解
status = prob.solve()
# 输出结果
if LpStatus[status] == 'Optimal':
    for v in prob.variables():
        print('%s %g' % (v.name, v.varValue))

    print('Obj: %g' % prob.objective.value())
=======
"""
混合整数规划
"""

from pulp import *
# 设置对象
prob = LpProblem(name='MIP', sense=LpMinimize)
# 设置变量
VAR_NUM = 3
variables = [LpVariable(name='x%d' % i, lowBound=0) for i in range(1, VAR_NUM)]
variables.append(LpVariable(name='x%d' % VAR_NUM, lowBound=0,
                            upBound=1, cat=const.LpInteger))
# 设置目标函数
c = [-3, -2, -1]
objective = sum([c[i]*variables[i] for i in range(0, VAR_NUM)])
# 约束条件
constraints = []
a1 = [1, 1, 1]
constraints.append(sum([a1[i]*variables[i] for i in range(0, VAR_NUM)]) <= 7)
a2 = [4, 2, 1]
constraints.append(sum([a2[i]*variables[i] for i in range(0, VAR_NUM)]) == 12)
a3 = [1, 0, 0]
constraints.append(sum([a3[i]*variables[i] for i in range(0, VAR_NUM)]) >= 0)
a4 = [0, 1, 0]
constraints.append(sum([a4[i]*variables[i] for i in range(0, VAR_NUM)]) >= 0)
# 载入变量
prob += objective
for cons in constraints:
    prob += cons
# 求解
status = prob.solve()
# 输出结果
if LpStatus[status] == 'Optimal':
    for v in prob.variables():
        print('%s %g' % (v.name, v.varValue))

    print('Obj: %g' % prob.objective.value())
>>>>>>> a66c8eec2c3bbe955d7da215f43ffffda9c7b6b5
